package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.seekting.demo_lib.MainActivity;

import java.io.IOException;

import okhttp3.Authenticator;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import seekting.demo2017.AppEnv;

/**
 * Created by seekting on 17-5-18.
 */

public class OKHttpActivity extends Activity {

    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = OKHttpActivity.class.getSimpleName();

    static {

        MainActivity.TITLE_MAP.put(OKHttpActivity.class, "");
        MainActivity.DESC_MAP.put(OKHttpActivity.class, "");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate.");
        }

        new Thread(new Runnable() {
            @Override
            public void run() {

                OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
                okBuilder.authenticator(new Authenticator() {
                    @Override
                    public Request authenticate(Route route, Response response) throws IOException {
                        Log.d("seekting", "OKHttpActivity.authenticate()" + response);
                        Request r= response.request();
                        return r;
                    }
                });
                OkHttpClient okHttpClient = okBuilder.build();
                Request.Builder requestbuilder = new Request.Builder();
                requestbuilder.url("http://192.168.1.108:8080");
                Request request = requestbuilder.build();
                try {
                    Response response = okHttpClient.newCall(request).execute();
                    String str = response.body().string();
                    if (DEBUG) {
                        Log.d(TAG, "run." + str);
                    }

                } catch (IOException e) {
                    e.printStackTrace();

                }


            }
        }).start();


    }
}
